#!/bin/bash
#
# Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
#

HELP="Initializes and brings up a virtual CAN interface.
Usage:
    `basename $0` <iface-name>
Example:
    `basename $0` vcan0"

function die() { echo $@ >&2; exit 1; }

if [ "$1" == '--help' ] || [ "$1" == '-h' ]; then echo "$HELP"; exit; fi
[ -n "$1" ] || die "Invalid usage. Use --help to get help."
[ "$(id -u)" == "0" ] || die "Must be root"

# ---------------------------------------------------------

IFACE="$1"

ip link show $IFACE > /dev/null
if [ $? == 0 ]; then
    ip link set up $IFACE
    exit
fi

modprobe can
modprobe can_raw
modprobe can_bcm
modprobe vcan

ip link add dev $IFACE type vcan
ip link set up $IFACE

echo "New iface $IFACE added successfully. To delete: ip link delete $IFACE"
